package cn.iocoder.yudao.module.esim.service.esapi;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.esim.framework.config.ESimartConfig;
import cn.iocoder.yudao.module.esim.service.esapi.model.CheckUsageRes;
import cn.iocoder.yudao.module.esim.service.esapi.model.CreateOrderReq;
import cn.iocoder.yudao.module.esim.service.esapi.model.CreateOrderRes;
import cn.iocoder.yudao.module.esim.service.esapi.model.ProductRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description: ESIMart
 * Author: li
 * Date: 2024/3/15
 */
@Service
@Slf4j
public class ESimThirtyApiServiceImpl implements IESimThirtyApiService {
    @Resource
    private ESimartConfig esimartConfig;

    @Override
    public ProductRes queryProduct() {
        final String url = esimartConfig.getHost() + esimartConfig.getGetProductUri();
        log.info("查询eSim产品-开始 url = {}", url);
        final HttpResponse response = HttpUtil.createGet(url).execute();
        if (response.isOk()) {
            final String responseJson = response.body();
            final ProductRes productResponse = JsonUtils.parseObject(responseJson, ProductRes.class);
            log.info("查询eSim产品-成功, responseJson = {}", responseJson);
            return productResponse;
        } else {
            final int statusCode = response.getStatus();
            log.info("查询eSim产品-失败, statusCode = {}, responseJson = {}", statusCode, response.body());
            throw new RuntimeException(String.format("ESIMart API query product failed, statusCode = %s", statusCode));
        }
    }

    @Override
    public CreateOrderRes createESim(CreateOrderReq req) {
        final String url = esimartConfig.getHost() + esimartConfig.getCreateOrderUri();
        final String requestJson = JsonUtils.toJsonString(req);
        log.info("eSim下单-开始 url = {}, requestJson = {}", url, requestJson);

        final HttpResponse response = HttpUtil.createPost(url).header("X-esimart-reseller-token", esimartConfig.getToken()).body(requestJson).execute();
        if (response.isOk()) {
            final String responseJson = response.body();
            log.info("eSim下单-成功, responseJson = {}", responseJson);
            return JsonUtils.parseObject(responseJson, CreateOrderRes.class);
        } else {
            final int statusCode = response.getStatus();
            log.info("eSim下单-失败, statusCode = {}, responseJson = {}", statusCode, response.body());
            throw new RuntimeException(String.format("ESIMart API order creation failed, statusCode = %s", statusCode));
        }
    }

    @Override
    public CheckUsageRes checkUsage(String eSimAssetId) {
        final String url = esimartConfig.getHost() + String.format(esimartConfig.getCheckUsageUri(), eSimAssetId);
        log.info("检查eSim使用量-开始 url = {}", url);

        final HttpResponse response = HttpUtil.createGet(url).header("X-esimart-reseller-token", esimartConfig.getToken()).execute();
        if (response.isOk()) {
            final String responseJson = response.body();
            log.info("检查eSim使用量-成功, responseJson = {}", responseJson);
            return JsonUtils.parseObject(responseJson, CheckUsageRes.class);
        } else {
            final int statusCode = response.getStatus();
            log.info("检查eSim使用量-失败, statusCode = {}, responseJson = {}", statusCode, response.body());
            throw new RuntimeException(String.format("ESIMart API check usage failed, statusCode = %s", statusCode));
        }
    }

}
